package com.txby.common.utils.util;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.base.payload.APNPayload.DictionaryAlertMsg;
import com.gexin.rp.sdk.base.payload.MultiMedia;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.NotyPopLoadTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import com.txby.common.data.CommonXGRetunInfo;

public class GTPushUtil {
	public static String appId = "SjKyeIRtSk9lgnLmBmrqc6";
    private static String appKey = "Odrkqk0kkw6JccJx7Rce43";
    private static String masterSecret = "vuAqcSvfT78d8KQ490WGG7";
    private static String CID = "1a6423abdcec5290bef570b5bf879cef";
    private static  IGtPush push;
	private static Map<Integer,Object>  customeParam =new HashMap();
//    static String CID = "ad9e2a8b1336181570b1f6b1d34e177b";
  //别名推送方式
   // static String Alias = "";
    static String host = "http://sdk.open.api.igexin.com/apiex.htm";
/*    public static void main(String[] args) throws Exception {
    	CID="cda97251ce532e194f40ee17a65b52d4";
    	CID="c43ebb12397409512967248df983e424";
         push = new IGtPush(host, appKey, masterSecret);
//        LinkTemplate template = linkTemplateDemo();
//        TransmissionTemplate template = transmissionTemplateDemo();
         Map content=new HashMap();
        
			Map data=new HashMap();
			data.put("callType", 0);
			data.put("discription", "紧急呼叫");
			data.put("msgId", 1);
			data.put("bedNO", "BJ11");
			data.put("sickAreaName", "陈集");
			content.put("data", data);
			content.put("callType", 0);
        TransmissionTemplate template = transmissionTemplateMessage(content);
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(CID);
        //target.setAlias(Alias);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            System.out.println(ret.getResponse().toString());
        } else {
            System.out.println("服务器响应异常");
        }
    	Map content=new HashMap();
        
		Map data=new HashMap();
		data.put("callType", 0);
		data.put("discription", "紧急呼叫");
		data.put("msgId", 1);
		data.put("bedNO", "BJ11");
		data.put("sickAreaName", "陈集");
		content.put("data", data);
		content.put("callType", 0);
		
		AppID： 
		EVirBcWtDR85AGeOWsYM99
		AppSecret： 
		qSG973sQxY7U0IpctNEoy7
		AppKey： 
		qXRIwGrdFr6pe42ybTAbZ9
		MasterSecret： 
		TZe409MRqB8TvnRI7yWeF7重置
		System.out.println(GTPushUtil.getNewInstance("SjKyeIRtSk9lgnLmBmrqc6", "Odrkqk0kkw6JccJx7Rce43", "vuAqcSvfT78d8KQ490WGG7").pushSingleDeviceCustomMessage("1a6423abdcec5290bef570b5bf879cef",content));
		System.out.println(GTPushUtil.getNewInstance("SjKyeIRtSk9lgnLmBmrqc6", "Odrkqk0kkw6JccJx7Rce43", "vuAqcSvfT78d8KQ490WGG7").pushSingleDeviceCustomMessage("1a6423abdcec5290bef570b5bf879cef","test","test"));
		
//		System.out.println(GTPushUtil.getNewInstance("SjKyeIRtSk9lgnLmBmrqc6", "Odrkqk0kkw6JccJx7Rce43", "vuAqcSvfT78d8KQ490WGG7"). pushSingleDeviceCustomMessage("c999b1bc7bc7921cc733d9239ac3e001", content));
//		System.out.println(GTPushUtil.getNewInstance("EVirBcWtDR85AGeOWsYM99", "qXRIwGrdFr6pe42ybTAbZ9", "TZe409MRqB8TvnRI7yWeF7").pushSingleDeviceCustomMessage("1a6423abdcec5290bef570b5bf879cef",content));
		System.out.println(GTPushUtil.getNewInstance("EVirBcWtDR85AGeOWsYM99", "qXRIwGrdFr6pe42ybTAbZ9", "TZe409MRqB8TvnRI7yWeF7").pushSingleDeviceCustomMessage("1a6423abdcec5290bef570b5bf879cef","test","test"));
		
    }*/
    
    public static synchronized GTPushUtil getNewInstance(String appId ,String appKey, String masterSecret){
    	  push = new IGtPush(host, appKey, masterSecret);
    	customeParam.put(0, "点餐提醒");
    	customeParam.put(1, "吃药提醒");
    	customeParam.put(2, "康复活动提醒");
    	return new GTPushUtil( appId , appKey,  masterSecret);
     
    }
    
    private GTPushUtil(String appId ,String appKey, String masterSecret){
    	this.appId=appId;
    	this.appKey=appKey;
    	this.masterSecret=masterSecret;
    
    }
    
    
    /**
     * 打开网页
     * @return
     */
    private static LinkTemplate linkTemplateDemo() {
        LinkTemplate template = new LinkTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);

        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle("请输入通知栏标题");
        style.setText("请输入通知栏内容");
        // 配置通知栏图标
        style.setLogo("icon.png");
        // 配置通知栏网络图标
        style.setLogoUrl("");
        // 设置通知是否响铃，震动，或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        template.setStyle(style);

        // 设置打开的网址地址
        template.setUrl("http://www.baidu.com");
        return template;
    }
    private static LinkTemplate linkTemplateMessage(String title,String text) {
    	LinkTemplate template = new LinkTemplate();
    	// 设置APPID与APPKEY
    	template.setAppId(appId);
    	template.setAppkey(appKey);
    	
    	Style0 style = new Style0();
    	// 设置通知栏标题与内容
    	style.setTitle(title);
    	style.setText(text);
    	// 配置通知栏图标
    	style.setLogo("icon.png");
   /* 	if(null!=icomName&&!"".equals(icomName)){
    	}*/
    	// 配置通知栏网络图标
    	style.setLogoUrl("");
    	// 设置通知是否响铃，震动，或者可清除
    	style.setRing(true);
    	style.setVibrate(true);
    	style.setClearable(true);
    	template.setStyle(style);
        // 设置打开的网址地址
        template.setUrl("http://www.baidu.com");
    	return template;
    }
    /**
     * 通知弹框
     * @return
     */
    private static NotyPopLoadTemplate notyPopLoadTemplateDemo() {
        NotyPopLoadTemplate template = new NotyPopLoadTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);

        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle("请输入通知栏标题");
        style.setText("请输入通知栏内容");
        // 配置通知栏图标
        style.setLogo("icon.png");
        // 配置通知栏网络图标
        style.setLogoUrl("");
        // 设置通知是否响铃，震动，或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        template.setStyle(style);

        // 设置弹框标题与内容
        template.setPopTitle("弹框标题");
        template.setPopContent("弹框内容");
        // 设置弹框显示的图片
        template.setPopImage("");
        template.setPopButton1("下载");
        template.setPopButton2("取消");
        // 设置下载标题
        template.setLoadTitle("下载标题");
        template.setLoadIcon("file://icon.png");
        //设置下载地址        
        template.setLoadUrl("http://gdown.baidu.com/data/wisegame/80bab73f82cc29bf/shoujibaidu_16788496.apk");
        // 设置定时展示时间
        // template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");
        return template;
    }
    /**
     * 通知弹框
     * @return
     */
    private static NotyPopLoadTemplate notyPopLoadTemplateMessage(String title,String text,String popTitle,String popContent,String icomName,String openUrl) {
    	NotyPopLoadTemplate template = new NotyPopLoadTemplate();
    	// 设置APPID与APPKEY
    	template.setAppId(appId);
    	template.setAppkey(appKey);
    	
    	Style0 style = new Style0();
    	// 设置通知栏标题与内容
    	style.setTitle("请输入通知栏标题");
    	style.setText("请输入通知栏内容");
    	// 配置通知栏图标
    	style.setLogo("icon.png");
    	// 配置通知栏网络图标
    	style.setLogoUrl("");
    	// 设置通知是否响铃，震动，或者可清除
    	style.setRing(true);
    	style.setVibrate(true);
    	style.setClearable(true);
    	template.setStyle(style);
    	
    	// 设置弹框标题与内容
    	template.setPopTitle("弹框标题");
    	template.setPopContent("弹框内容");
    	// 设置弹框显示的图片
    	template.setPopImage("");
    	template.setPopButton1("下载");
    	template.setPopButton2("取消");
    	// 设置下载标题
    	template.setLoadTitle("下载标题");
    	template.setLoadIcon("file://icon.png");
    	//设置下载地址        
    	template.setLoadUrl("http://gdown.baidu.com/data/wisegame/80bab73f82cc29bf/shoujibaidu_16788496.apk");
    	// 设置定时展示时间
    	// template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");
    	return template;
    }
    /**
     * 打开应用
     * @return
     */
    private static NotificationTemplate notificationTemplateDemo() {
        NotificationTemplate template = new NotificationTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);
        // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
        template.setTransmissionType(1);
        template.setTransmissionContent("请输入您要透传的内容");
        // 设置定时展示时间
        // template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");

        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle("请输入通知栏标题");
        style.setText("请输入通知栏内容");
        // 配置通知栏图标
        style.setLogo("icon.png");
        // 配置通知栏网络图标
        style.setLogoUrl("");
        // 设置通知是否响铃，震动，或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        template.setStyle(style);

        return template;
    }

    /**
     * 透传消息
     * @return
     */
    private static TransmissionTemplate transmissionTemplateDemo() {
    	TransmissionTemplate template = new TransmissionTemplate();
    	template.setAppId(appId);
    	template.setAppkey(appKey);
    	// 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
    	template.setTransmissionType(2);
    	template.setTransmissionContent("请输入需要透传的内容");
    	// 设置定时展示时间
    	// template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");
    	return template;
    }
    /**
     * 透传消息
     * @return
     */
    private static TransmissionTemplate transmissionTemplateMessage(Map content) {
    	TransmissionTemplate template = new TransmissionTemplate();
    	template.setAppId(appId);
    	template.setAppkey(appKey);
    	// 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
    	template.setTransmissionType(2);
    	template.setTransmissionContent(ResponseUtils.toJSONString(content));
    	// 设置定时展示时间
    	// template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");
    	return template;
    }
    /**
     * 透传消息
     * @param token 设备token必填
     * @param content
     * @return
     */
    public CommonXGRetunInfo pushSingleDeviceCustomMessage (String token, Map content) {
/*//        LinkTemplate template = linkTemplateDemo();
//        TransmissionTemplate template = transmissionTemplateDemo();
    	 AbstractTemplate template=null;
    	if(msgType==0){
    		template = linkTemplateDemo();
    	}else{
    		template = transmissionTemplateMessage(content);
    	}*/
    		TransmissionTemplate template = transmissionTemplateMessage(content);
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
      /*  message.setOfflineExpireTime(24 * 3600 * 1000);*/
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(token);
        //target.setAlias(Alias);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
            Map m=ret.getResponse();
            System.out.println(m);
          return new CommonXGRetunInfo().resultMessage(0, "", m.get("result").toString());
        } catch (RequestException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 通知栏消息
     * @param token
     * @param title
     * @param text
     * @return
     */
    public CommonXGRetunInfo pushSingleDeviceCustomMessage (String token, String title,String text) {
       LinkTemplate template = linkTemplateMessage(title, text);
    	SingleMessage message = new SingleMessage();
    	message.setOffline(true);
    	// 离线有效时间，单位为毫秒，可选
    	/*  message.setOfflineExpireTime(24 * 3600 * 1000);*/
    	message.setData(template);
    	// 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
    	message.setPushNetWorkType(0);
    	Target target = new Target();
    	target.setAppId(appId);
    	target.setClientId(token);
    	//target.setAlias(Alias);
    	IPushResult ret = null;
    	try {
    		ret = push.pushMessageToSingle(message, target);
//    		ret = push.pushMessageToSingle(message, target);
    		  Map m=ret.getResponse();
              System.out.println(m);
            return new CommonXGRetunInfo().resultMessage(0, "", m.get("result").toString());
    	} catch (RequestException e) {
    		e.printStackTrace();
    		return null;
    	}
    }
    /**
     * 获取ios消息模版，如果app 在线则透传，否则通知栏
    * @Title: getIOSTransmissionTemplate 
    * @Description: TODO(这里用一句话描述这个方法的作用) 
    * @param @param customMessage
    * @param @param title
    * @param @param text
    * @param @return    设定文件 
    * @return TransmissionTemplate    返回类型 
    * @throws
     */
    private static TransmissionTemplate getIOSTransmissionTemplate(Map customMessage,String title,String text) {
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTransmissionContent(JSON.toJSONString(customMessage));
        template.setTransmissionType(2);
        APNPayload payload = new APNPayload();
        //在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
        payload.setAutoBadge("+1");
        payload.setContentAvailable(1);
        payload.setSound("default");
        payload.setCategory("约定字段");

        //简单模式APNPayload.SimpleMsg
//        payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));

        //字典模式使用APNPayload.DictionaryAlertMsg
        payload.setAlertMsg(getDictionaryAlertMsg(title,text));
        // 添加多媒体资源
        payload.addMultiMedia(new MultiMedia().setResType(MultiMedia.MediaType.video)
                    .setResUrl("http://ol5mrj259.bkt.clouddn.com/test2.mp4")
                    .setOnlyWifi(true));

        template.setAPNInfo(payload);
    	SingleMessage message = new SingleMessage();
    	message.setData(template);
        return template;
    }
    /**
     * 获取ios通知栏信息
    * @Title: getDictionaryAlertMsg 
    * @Description: TODO(这里用一句话描述这个方法的作用) 
    * @param @param titile
    * @param @param text
    * @param @return    设定文件 
    * @return DictionaryAlertMsg    返回类型 
    * @throws
     */
    private static  DictionaryAlertMsg getDictionaryAlertMsg(String titile,String text){
         DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
        alertMsg.setBody(text);
        alertMsg.setActionLocKey("ActionLockey");
        alertMsg.setLocKey(titile);
        alertMsg.addLocArg("loc-args");
        alertMsg.setLaunchImage("launch-image");
        // iOS8.2以上版本支持
        alertMsg.setTitle(titile);
        alertMsg.setTitleLocKey("TitleLocKey");
        alertMsg.addTitleLocArg("TitleLocArg");
        return alertMsg;
    }
    
    
    /**
     * iso 推送自定义消息
    * @Title: pushIOSSingleDeviceCustomMessage 
    * @Description: TODO(这里用一句话描述这个方法的作用) 
    * @param @param token
    * @param @param customMessage
    * @param @param title
    * @param @param text
    * @param @return    设定文件 
    * @return CommonXGRetunInfo    返回类型 
    * @throws
     */
    public static CommonXGRetunInfo pushIOSSingleDeviceCustomMessage(String token,Map customMessage,String title,String text ){
    	TransmissionTemplate template = getIOSTransmissionTemplate(customMessage, title, text);
    	 SingleMessage message = new SingleMessage();
         message.setOffline(true);
         // 离线有效时间，单位为毫秒，可选
       /*  message.setOfflineExpireTime(24 * 3600 * 1000);*/
         message.setData(template);
         // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
         message.setPushNetWorkType(0);
         Target target = new Target();
         target.setAppId(appId);
         target.setClientId(token);
         IPushResult ret = null;
         try {
             ret = push.pushMessageToSingle(message, target);
             Map m=ret.getResponse();
//             System.out.println(m);
           return new CommonXGRetunInfo().resultMessage(0, "", m.get("result").toString());
         } catch (RequestException e) {
             e.printStackTrace();
           
         }
		return null;
    	
    }
    public static void main(String[] args) {
	Map content=new HashMap();
		Map data=new HashMap();
		data.put("callType", 0);
		data.put("discription", "紧急呼叫");
		data.put("msgId", 1);
		data.put("bedNO", "BJ11");
		data.put("sickAreaName", "陈集");
		content.put("data", data);
		content.put("callType", 0);
		    String token="1a6423abdcec5290bef570b5bf879cef";
//    	System.out.println(GTPushUtil.getNewInstance("EVirBcWtDR85AGeOWsYM99", "qXRIwGrdFr6pe42ybTAbZ9", "TZe409MRqB8TvnRI7yWeF7").pushSingleDeviceCustomMessage("1a6423abdcec5290bef570b5bf879cef","test标题","test内容"));
    	System.out.println(GTPushUtil.getNewInstance("EVirBcWtDR85AGeOWsYM99", "qXRIwGrdFr6pe42ybTAbZ9", "TZe409MRqB8TvnRI7yWeF7").pushIOSSingleDeviceCustomMessage(token,content,"test标题","test内容"));
//		System.out.println(GTPushUtil.getNewInstance("SjKyeIRtSk9lgnLmBmrqc6", "Odrkqk0kkw6JccJx7Rce43", "vuAqcSvfT78d8KQ490WGG7").pushSingleDeviceCustomMessage("a83ee551cdf99b9d42db4f4cb9770500",content));
//		System.out.println(GTPushUtil.getNewInstance("EVirBcWtDR85AGeOWsYM99", "qXRIwGrdFr6pe42ybTAbZ9", "TZe409MRqB8TvnRI7yWeF7").pushSingleDeviceCustomMessage("1a6423abdcec5290bef570b5bf879cef",content));
//    	
	}

	public CommonXGRetunInfo pushIOSSingleDeviceCustomMessage(String gtToken, Map customMessage, String title, Map custom,Integer num) {
		TransmissionTemplate template = getIOSTransmissionTemplate(customMessage, title, custom,num);
   	 SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
      /*  message.setOfflineExpireTime(24 * 3600 * 1000);*/
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(gtToken);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
            Map m=ret.getResponse();
           System.out.println(m);
          return new CommonXGRetunInfo().resultMessage(0, "", m.get("result").toString());
        } catch (RequestException e) {
            e.printStackTrace();
          
        }
		return null;
	}

	private TransmissionTemplate getIOSTransmissionTemplate(Map customMessage, String title, Map custom,Integer num) {
		 TransmissionTemplate template = new TransmissionTemplate();
	        template.setAppId(appId);
	        template.setAppkey(appKey);
	        template.setTransmissionContent(JSON.toJSONString(customMessage));
	        template.setTransmissionType(2);
	        APNPayload payload = new APNPayload();
	        //在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
	       // payload.setAutoBadge("+1");
	        payload.setBadge(num);
	        payload.setContentAvailable(1);
	        payload.setSound("default");
	      //  payload.setCategory(title);
	        //payload.setAlertMsg(title);
	        payload.addCustomMsg("msg", custom);
	        //简单模式APNPayload.SimpleMsg
	        payload.setAlertMsg(new APNPayload.SimpleAlertMsg(title));
	        //字典模式使用APNPayload.DictionaryAlertMsg
	       // payload.setAlertMsg(getDictionaryAlertMsg(title,custom));
	        // 添加多媒体资源
	        payload.addMultiMedia(new MultiMedia().setResType(MultiMedia.MediaType.video)
	                    .setResUrl("http://ol5mrj259.bkt.clouddn.com/test2.mp4")
	                    .setOnlyWifi(true));

	        template.setAPNInfo(payload);
	    	SingleMessage message = new SingleMessage();
	    	message.setData(template);
	        return template;
	}

	

    
    
}